<?php

class InsuranceAttendForm extends BaseForm
{

    private $insuranceAttendService;
    private $insuranceAttendId;

    /**
     * @param InsuranceAttendService $insuranceAttendService
     */
    public function setInsuranceAttendService(InsuranceAttendService $insuranceAttendService)
    {
        $this->insuranceAttendService = $insuranceAttendService;
    }

    /**
     * @return InsuranceAttendService
     */
    public function getInsuranceAttendService()
    {
        if (is_null($this->insuranceAttendService)) {
            $this->insuranceAttendService = new InsuranceAttendService();
            $this->insuranceAttendService->setInsuranceAttendDao(new InsuranceAttendDao());
        }
        return $this->insuranceAttendService;
    }

    public function configure()
    {

        $this->insuranceAttendId = $this->getOption('insuranceAttendId');
        if (isset($this->insuranceAttendId)) {
            $insuranceAttend = $this->getInsuranceAttendDetails($this->insuranceAttendId);
        }

        // creating widgets
        $this->setWidgets(array(
            'name' => new sfWidgetFormInputText(),
            'description' => new sfWidgetFormTextArea(),
            'regulation' => new sfWidgetFormTextArea(),
            'family' => new sfWidgetFormTextArea(),
            'note' => new sfWidgetFormTextArea(),
            'creationDate' => new sfWidgetFormInputText(),
        ));

        $this->setValidators(array(
            'name' => new sfValidatorString(array('required' => true, 'max_length' => 100)),
            'description' => new sfValidatorString(array('required' => true, 'max_length' => 500)),
            'regulation' => new sfValidatorString(array('required' => true, 'max_length' => 100)),
            'family' => new sfValidatorString(array('required' => false, 'max_length' => 400)),
            'note' => new sfValidatorString(array('required' => false, 'max_length' => 200)),
        ));

        $this->widgetSchema->setNameFormat('addInsuranceAttend[%s]');

        if (isset($insuranceAttend) && $insuranceAttend != null) {
            $this->setDefault('name', $insuranceAttend->getName());
            $this->setDefault('description', $insuranceAttend->getDescription());
            $this->setDefault('regulation', $insuranceAttend->getRegulation());
            $this->setDefault('family', $insuranceAttend->getFamily());
            $this->setDefault('note', $insuranceAttend->getNote());
            $this->setDefault('creationDate', $insuranceAttend->getCreationDate());
        }
    }

    public function save()
    {
        if (empty($this->insuranceAttendId)) {
            $insuranceAttend = new InsuranceAttend();
            $insuranceAttend->creationDate = date('Y-m-d H:i:s');
        }

        $insuranceAttend->name = $this->getValue('name');
        $insuranceAttend->description = $this->getValue('description');
        $insuranceAttend->regulation = $this->getValue('regulation');
        $insuranceAttend->family = $this->getValue('family');
        $insuranceAttend->note = $this->getValue('note');

        $this->getInsuranceAttendService()->saveInsuranceAttend($insuranceAttend);

        return $insuranceAttend->getId();
    }

    public function getInsuranceAttendList()
    {
        $list = array();
        $insuranceAttendList = $this->getInsuranceAttendService()->getInsuranceAttendList();
        foreach ($insuranceAttendList as $insuranceAttend) {
            $list[] = array('id' => $insuranceAttend->getId(), 'name' => $insuranceAttend->getName());
        }

        return json_encode($list);
    }

    private function getInsuranceAttendDetails($insuranceAttendId)
    {
        return $this->getInsuranceAttendService()->getInsuranceAttendById($insuranceAttendId);
    }
}